module exp3(
	input wire[7:0] mode_choose,
	input wire[7:0] input_data,
	input wire clk,
	input wire[3:0] button_hang,
	output reg[3:0] button_lie,
	output reg[5:0] wei_xuan,
	output reg[7:0] duan_xuan
);
	reg[7:0] is_enable;
	reg[7:0] number_dict[9:0];
	integer section_ticks=1,global_ticks=0,bkp_ticks = 0;
	integer digital_index=0;
	integer index = 0;
	integer tick_lock = 0;
	integer temp;
	integer hang_index=0,lie_index=0,button_index=0;
	reg[15:0] button_time[15:0];
	reg[15:0] button_state = 0;
	reg section_clk;
	reg[3:0] out_number[5:0];
	
	wire exp3_1_out;
	rs_trigger exp3_1(.clk(section_clk),.q(exp3_1_out),.r(input_data[0]),.s(input_data[1]),.rst(~is_enable[0]));
	
	wire exp3_2_out;
	jk_trigger exp3_2(.clk(section_clk),.q(exp3_2_out),.j(input_data[0]),.k(input_data[1]),.rst(~is_enable[1]));

	wire exp3_3_out;
	d_trigger exp3_3(.clk(section_clk),.q(exp3_3_out),.in(input_data[0]),.rst(~is_enable[2]));
	
	reg count_clock=0;
	wire[4:0] exp3_4_out;
	syn_cnt10 exp3_4(.clk(button_state[0]),.q(exp3_4_out),.rst(~is_enable[3]));
	
	wire[3:0] exp3_5_out;
	rip_cnt10 exp3_5(.clk(count_clock),.q(exp3_5_out),.rst(~is_enable[4]));

	wire[3:0] exp3_6_out;
	keni_cnt10 exp3_6(.clk(count_clock),.q(exp3_6_out),.rst(~is_enable[5]));
	
	//wire[3:0] exp3_7_out;
	//yiwei_register exp3_7(.clk(count_clock),.din(input_data[3:0]),.rst(~is_enable[6]),.dout(exp3_7_out));
	
	wire[3:0] exp3_7_out;
	sx_register exp3_7(.clk(count_clock),.din(input_data[3:0]),.rst(~is_enable[6]),.dout(exp3_7_out),.s0(input_data[4]),.s1(input_data[5]));
	
	wire exp3_8_out;
	xu_lie_gen exp3_8(.clk(count_clock),.din(input_data[3:0]),.rst(~is_enable[7]),.dout(exp3_8_out));
	
	initial begin
		wei_xuan = ~0;
		duan_xuan = ~0;
		number_dict[0] = ~(8'h3F);
		number_dict[1] = ~(8'h06);
		number_dict[2] = ~(8'h5B);
		number_dict[3] = ~(8'h4F);
		number_dict[4] = ~(8'h66);
		number_dict[5] = ~(8'h6D);
		number_dict[6] = ~(8'h7D);
		number_dict[7] = ~(8'h07);
		number_dict[8] = ~(8'h7F);
		number_dict[9] = ~(8'h6F);
		for (index=0;index<6;index =index+1) begin
			out_number[index] = 0;
		end
		for (index=0;index<16;index =index+1) begin
			button_time[index] = 0;
		end
		button_lie = 4'b1111;
	end
	
	always @(posedge clk) begin
		if(tick_lock == 0) begin
			section_ticks = section_ticks + 1;
			if (section_ticks % 12_250 == 0) begin
			
				wei_xuan = 6'b111111;
				wei_xuan[digital_index] = 0;
				duan_xuan = number_dict[out_number[digital_index]];
				
				digital_index = digital_index + 1;
				digital_index = digital_index % 6;
			end
			if (section_ticks % 50_00 == 0) begin
				section_clk = ~section_clk;
			end
			if (section_ticks % 50_000 == 0) begin //50_000
				button_state = 0;
				lie_index = button_index % 4;
				hang_index = (button_index-lie_index)/4;

				
				button_lie = 4'b1111;
				button_lie[lie_index] = 0;

				if (button_hang[hang_index]==0) begin
					button_time[button_index]=button_time[button_index]+1;
				end
				else begin
					if(button_time[button_index]>=20) begin
							button_state[button_index] = 1;
							//for(temp=0;temp<16;temp=temp+1)
								//button_time[button_index]=0;
					end
					//for(temp=0;temp<16;temp=temp+1)
					button_time[button_index]=0;
				end
				
				button_index = button_index + 1;
				button_index = button_index % 16;
			end
			if (section_ticks % 25_000_000 == 0) begin
				count_clock = ~count_clock;
				section_ticks = 1;
			end
		end
	end
	
	
	always @(mode_choose) begin
		is_enable = 0;
		case(mode_choose)
		8'b00000000:is_enable = 0;
		8'b00000001:is_enable[0] = 1;
		8'b00000010:is_enable[1] = 1;
		8'b00000100:is_enable[2] = 1;
		8'b00001000:is_enable[3] = 1;
		8'b00010000:is_enable[4] = 1;
		8'b00100000:is_enable[5] = 1;
		8'b01000000:is_enable[6] = 1;
		8'b10000000:is_enable[7] = 1;
		endcase
	end
	
	always @(exp3_1_out or exp3_2_out or exp3_3_out or exp3_4_out or exp3_5_out or exp3_6_out or exp3_7_out or exp3_8_out) begin
		for (index=0;index<6;index =index+1) begin
			out_number[index] = 0;
		end
		case(is_enable)
		8'b00000001:out_number[0] = {2'b00,exp3_1_out};
		8'b00000010:out_number[0] = {2'b00,exp3_2_out};
		8'b00000100:out_number[0] = {2'b00,exp3_3_out};
		8'b00001000:begin
		out_number[0] = exp3_4_out/10;out_number[1] = exp3_4_out%10;
		end
		8'b00010000:out_number[0] = exp3_5_out;
		8'b00100000:out_number[0] = exp3_6_out;
		8'b01000000:begin
		out_number[0] = exp3_7_out/10;out_number[1] = exp3_7_out%10;
		end
		8'b10000000:begin
		out_number[0] = exp3_8_out;
		end
		default:begin
			for (index=0;index<6;index =index+1) begin
				out_number[index] = 0;
			end
		end
		endcase
	end
	

endmodule
